package com.aegean.MonitorExamSYS.center.action;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.aspectj.util.FileUtil;
import org.hibernate.Hibernate;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.center.business.CenterBusiness;
import com.aegean.MonitorExamSYS.tools.MyTools;
import com.aegean.MonitorExaxSYS.model.ExamInfo;
import com.aegean.MonitorExaxSYS.model.component.Photo;
import com.opensymphony.xwork2.ActionSupport;

@Controller("CUDExamValue")
@Scope("prototype")
public class CUDExamValue extends ActionSupport {
	@Resource
	CenterBusiness business;
	private ExamInfo examValue;
	private File exam_Photo;
	private String exam_PhotoFileName;
	private String exam_PhotoContentType;
	private boolean success;
	private int small_Id;
	public ExamInfo getExamValue() {
		return examValue;
	}

	public void setExamValue(ExamInfo ExamValue) {
		this.examValue = ExamValue;
	}

	public int getSmall_Id() {
		return small_Id;
	}

	public void setSmall_Id(int small_Id) {
		this.small_Id = small_Id;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public File getExam_Photo() {
		return exam_Photo;
	}

	public void setExam_Photo(File exam_Photo) {
		this.exam_Photo = exam_Photo;
	}

	public String getExam_PhotoFileName() {
		return exam_PhotoFileName;
	}

	public void setExam_PhotoFileName(String exam_PhotoFileName) {
		this.exam_PhotoFileName = exam_PhotoFileName;
	}

	public String getExam_PhotoContentType() {
		return exam_PhotoContentType;
	}

	public void setExam_PhotoContentType(String exam_PhotoContentType) {
		this.exam_PhotoContentType = exam_PhotoContentType;
	}

	public String add() throws Exception {
		String realPath = ServletActionContext.getServletContext().getRealPath(
				"/dateFile/examInfo/" + small_Id);
		File file_Small = new File(realPath);
		if (!file_Small.exists()) {
			file_Small.mkdir();
		}
		File file_Photo = new File(realPath, exam_PhotoFileName);
		FileUtil.copyFile(exam_Photo, file_Photo);
		Photo photo = new Photo();
		photo.setPhoto(Hibernate.createBlob(new FileInputStream(exam_Photo)));
		photo.setPhoto_Name(exam_PhotoFileName);
		photo.setPhoto_Type(exam_PhotoContentType);
		examValue.setExamInfo_Photo(photo);
		examValue.setExamInfo_CreateTime(new Date());
		success = business.addExamValue(examValue, small_Id);
		return SUCCESS;
	}

	public String edit() throws Exception {
		if (exam_Photo != null) {
			String realPath = ServletActionContext.getServletContext()
					.getRealPath("/dateFile/examInfo/" + small_Id);
			File file_Small = new File(realPath);
			if (!file_Small.exists()) {
				file_Small.mkdir();
			}
			File file_Photo = new File(realPath, exam_PhotoFileName);
			FileUtil.copyFile(exam_Photo, file_Photo);
			Photo photo = new Photo();
			photo.setPhoto(Hibernate
					.createBlob(new FileInputStream(exam_Photo)));
			photo.setPhoto_Name(exam_PhotoFileName);
			photo.setPhoto_Type(exam_PhotoContentType);
			examValue.setExamInfo_Photo(photo);
		}
		success = business.editExamValue(examValue, exam_Photo);
		return SUCCESS;
	}
}
